package top.fyxxxx.dbdesign.util;

import top.fyxxxx.dbdesign.dao.DBoperate;
import top.fyxxxx.dbdesign.data.Course;
import top.fyxxxx.dbdesign.data.PersonalData;
import top.fyxxxx.dbdesign.data.SignInTask;
import top.fyxxxx.dbdesign.data.TaskData;

import java.util.ArrayList;
import java.util.List;

public class Reporter implements QueryModule{
    private DBoperate dBoperate=DBoperate.getInstance();
    private static Reporter instance=new Reporter();
    private Reporter(){}
    public static Reporter getInstance(){
        return instance;
    }
    @Override
    public List<PersonalData> getStudentsData(long uid, long cid) {
        //没有权限返回null
        Course course=dBoperate.getCourse(cid);
        if(!course.getPowerUid().contains(uid))return null;
        ArrayList<PersonalData> list=new ArrayList<>();
        for(long mem:course.getMemberUid()){
            if(course.getPowerUid().contains(mem))continue;
            list.add(dBoperate.getPersonalData(mem,cid));
        }
        return list;
    }

    @Override
    public List<TaskData> getTasksData(long uid, long cid) {
        //没有权限返回null
        Course course=dBoperate.getCourse(cid);
        if(!course.getPowerUid().contains(uid))return null;
        ArrayList<TaskData>list=new ArrayList<>();
        for(long sid:course.getTaskList()){
            list.add(dBoperate.getTaskData(sid));
        }
        return list;
    }

    @Override
    public PersonalData getPersonalData(long uid,long who, long cid) {
        //没有权限返回null
        Course course=dBoperate.getCourse(cid);
        if(!course.getPowerUid().contains(uid)&&uid!=who)return null;
        return dBoperate.getPersonalData(who,cid);
    }

    @Override
    public boolean issignin(long uid, long sid) {
        return dBoperate.getSignInRecord(uid,sid).getSdate()!=0;
    }
    public boolean studentBelongToCourse(long uid,Course course){
        DBoperate dBoperate=DBoperate.getInstance();
        return course.getMemberUid().contains(uid);
    }
    public  String[] getSignInTaskInformation(long sid){
        String ans[]=new String[3];
        SignInTask signInTask=dBoperate.getSignInTask(sid);
        String begin= SysUtil.getTime(signInTask.getBeginTime());
        String end=SysUtil.getTime(signInTask.getEndTime());
        long uid=signInTask.getCreateByUid();
        String name=dBoperate.getAccountByUid(uid).getName();
        ans[0]=name;ans[1]=begin;ans[2]=end;
        return ans;
    }
    public long getCidBySid(long sid){
        return dBoperate.getSignInTask(sid).getBelongToCid();
    }
    public boolean checkSignTaskTime(long sid){
        SignInTask task=dBoperate.getInstance().getSignInTask(sid);
        if(task.getEndTime()<System.currentTimeMillis()||
                task.getBeginTime()>System.currentTimeMillis())return false;
        return true;
    }
    public  boolean checkAdmin(long uid,long cid){
        return (dBoperate.getInstance().getPersonalData(uid,cid).getPower()&2)!=0;
    }
}
